interface HttpRespones {
	code: number,
	msg: string,
	data: any
}

type TMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';


class HttpRequest {
	// host = 'https://test-api.paleisi.com' 
	// host = 'https://api.paleisi.com' 
	host = 'http://43.143.226.90:8880' 
	constructor() {
		
	}
	getToken = () :string =>  {
		let token = JSON.parse(uni.getStorageSync('loginInfo') || "{}") 
		return token.accessToken || ''
	}
	request = (url: string, method: TMethods = 'GET', data: any = {}): Promise<HttpRespones> | any => { 
		return new Promise((resolve, inject) => {
			uni.request({
				url: this.host + url,
				method ,
				data, 
				header: {
					'Access-Control-Allow-Origin': '*',
					Authorization: `Bearer ${this.getToken()}` || ''
				},
				success(res) { 
					if(res.statusCode == 401){
						uni.clearStorage()
						uni.navigateTo({ url: '/pages/login/index' })
					}
					resolve(res.data)
				},
				fail(err) { 
					inject(err)
				},
			})
		})
	}
	// 无需header
	http = (url: string, method: TMethods = 'GET', data: any = {}): Promise<HttpRespones> | any => {  
		return new Promise((resolve, inject) => {
			uni.request({
				url: this.host + url,
				method ,
				data,
				success(res) {
					resolve(res.data)
				},
				fail(err) {
					console.log(err, 8989);
					inject(err)
				},
			})
		})
	}
}

export const request = new HttpRequest().request
export const http = new HttpRequest().http